package com.ruoyi.tongban.service.impl;

import java.util.List;

import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.tongban.domain.TInstitution;
import com.ruoyi.tongban.mapper.TInstitutionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.tongban.mapper.FaceDeviceMapper;
import com.ruoyi.tongban.domain.FaceDevice;
import com.ruoyi.tongban.service.IFaceDeviceService;

/**
 * 设备信息表Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-04-08
 */
@Service
public class FaceDeviceServiceImpl implements IFaceDeviceService
{
    @Autowired
    private FaceDeviceMapper faceDeviceMapper;

    @Autowired
    private TInstitutionMapper tInstitutionMapper;

    /**
     * 查询设备信息表
     * 
     * @param deviceId 设备信息表ID
     * @return 设备信息表
     */
    @Override
    public FaceDevice selectFaceDeviceById(Long deviceId)
    {
        return faceDeviceMapper.selectFaceDeviceById(deviceId);
    }

    /**
     * 查询设备信息表列表
     * 
     * @param faceDevice 设备信息表
     * @return 设备信息表
     */
    @Override
    public List<FaceDevice> selectFaceDeviceList(FaceDevice faceDevice)
    {
        return faceDeviceMapper.selectFaceDeviceList(faceDevice);
    }

    @Override
    public FaceDevice findBySn(String sn) {
        return faceDeviceMapper.findBySn(sn);
    }

    @Override
    public FaceDevice findByDeptId(Long deptId) {
        return faceDeviceMapper.findByDeptId(deptId);
    }


    /**
     * 新增设备信息表
     * 
     * @param faceDevice 设备信息表
     * @return 结果
     */
    @Override
    public int insertFaceDevice(FaceDevice faceDevice)
    {
        if(faceDevice.getSn() == null){
            throw new CustomException("添加失败,请填写设备序列号！");
        }
        String sn = faceDevice.getSn();
        if(faceDeviceMapper.findBySn(sn) != null){
            throw new CustomException("添加失败,尝试添加的" + sn + "已存在！");
        }

        Long institutionId = faceDevice.getInstitutionId();
        TInstitution result = tInstitutionMapper.selectTInstitutionById(institutionId);
        if(result == null){
            throw new CustomException("添加失败,请选择机构列表!");
        }
        Long deptId = result.getInstitutionId();
        String deptName = result.getInstitutionName();
        if(faceDeviceMapper.findByDeptId(deptId) != null){
            throw new CustomException("添加失败,添加的" + deptName + "重复！");
        }
        faceDevice.setDeptId(deptId);
        faceDevice.setDeptName(deptName);
        faceDevice.setCreateTime(DateUtils.getNowDate());
        return faceDeviceMapper.insertFaceDevice(faceDevice);
    }

    /**
     * 修改设备信息表
     * 
     * @param faceDevice 设备信息表
     * @return 结果
     */
    @Override
    public int updateFaceDevice(FaceDevice faceDevice)
    {

        Long institutionId = faceDevice.getInstitutionId();
        TInstitution result = tInstitutionMapper.selectTInstitutionById(institutionId);
        if(result == null){
            throw new CustomException("更改失败,请选择机构列表!");
        }

        Long deptId = result.getInstitutionId();
        String deptName = result.getInstitutionName();

        if(faceDeviceMapper.findByDeptId(deptId) != null){
            throw new CustomException("更改失败,更改的" + deptName + "已存在！请选择其它幼儿园");
        }

        faceDevice.setDeptId(deptId);
        faceDevice.setDeptName(deptName);
        faceDevice.setCreateTime(DateUtils.getNowDate());
        return faceDeviceMapper.updateFaceDevice(faceDevice);
    }

    /**
     * 批量删除设备信息表
     * 
     * @param deviceIds 需要删除的设备信息表ID
     * @return 结果
     */
    @Override
    public int deleteFaceDeviceByIds(Long[] deviceIds)
    {
        return faceDeviceMapper.deleteFaceDeviceByIds(deviceIds);
    }

    /**
     * 删除设备信息表信息
     * 
     * @param deviceId 设备信息表ID
     * @return 结果
     */
    @Override
    public int deleteFaceDeviceById(Long deviceId)
    {
        return faceDeviceMapper.deleteFaceDeviceById(deviceId);
    }
}
